package com.arturjaneczko.view.impl.clipboard.preview;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import com.arturjaneczko.model.core.IModel;
import com.arturjaneczko.view.core.AView;
import com.arturjaneczko.view.core.IView;

public class PanelPreview extends AView {
	
	private Class<Container> $class;
	private JPanel panel;

	public PanelPreview(IModel model) {
		super(model);
		compositeList = new ArrayList<>();
	}

	@Override
	public void ui() {
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//		GridBagConstraints gridBagConstraints = new GridBagConstraints();
//		gridBagConstraints.fill = GridBagConstraints.VERTICAL;
//		gridBagConstraints.gridx = 0;
//		gridBagConstraints.gridy = 0;
//		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setBackground(java.awt.Color.white);
				
		JScrollPane scrollPane = new JScrollPane(panel);
		scrollPane.setBackground(java.awt.Color.green);
		scrollPane.setPreferredSize(new Dimension(108, 120));
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		uiComposite(this, scrollPane, null);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void uiComposite(IView viewComposite, Container parentContainer,
			Object constraint) {
		try {
			$class = (Class<Container>) Class.forName(viewComposite.getClass()
					.getPackage().getName()
					+ "." + viewComposite.getClass().getSimpleName());
			Method m = $class.getMethod("addChild", Container.class,
					Container.class, Object.class);
			if (viewComposite.getParent() != null) {
				m.invoke(viewComposite, viewComposite.getParent(),
						parentContainer, constraint);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		Iterator<IView> iterator = compositeList.iterator();
		while (iterator.hasNext()) {
			IView iView = iterator.next();
			iView.setParent(panel);
			iView.ui();
		}
	}

	@Override
	public void update(Observable o, Object arg) {

		// for (ClipboardObject clipboardObject : model
		// .getStdClipboardCollection()) {
		// DefaultListModel defaultListModel = new DefaultListModel<>();
		// }
	}

}
